package com.rachel.commonsdk.custom.bannerview;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhengshaorui on 2017/8/17.
 */

public class BannerPagerView extends ViewPager {
    private static final String TAG = "zsr";
    private List<View> mDatas = new ArrayList<>();

    public BannerPagerView(Context context) {
        super(context);
    }

    public BannerPagerView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    public void setDatas(List<View> lists){
        //数据 加 2，达到一个循环效果,用首尾填充的方式
        int count = lists.size();
        for (int i = 0; i < count + 2; i++) {
            if (i == 0){
                mDatas.add(lists.get(count -1 ));
            }else if (i == (count + 1)){
                mDatas.add(lists.get(0));
            }else{
                mDatas.add(lists.get(i - 1));
            }
        }


        setCurrentItem(mDatas.size()/2);
        setOffscreenPageLimit(3);
        setAdapter(new MypageAdapter());

        this.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {
                if (state == ViewPager.SCROLL_STATE_IDLE){
                    int currentid = getCurrentItem();
                    if (currentid == mDatas.size() - 1){
                        setCurrentItem(1,false);
                    }else if (currentid == 0){
                        setCurrentItem(mDatas.size() - 1,false);
                    }
                }
            }
        });
    }



    class MypageAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            return mDatas.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view ==  object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = mDatas.get(position);
            ViewParent viewParent =  view.getParent();
            Log.d(TAG, "instantiateItem: "+viewParent);
            if (viewParent != null){
                ViewGroup viewGroup = (ViewGroup) viewParent.getParent();
                ViewGroup viewGroup1 = (ViewGroup) viewGroup.getParent();
                viewGroup1.removeView(viewGroup);
            }
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}
